﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using JX.Application;
using JX.Core.Entity;
using JX.Infrastructure;
using JX.Infrastructure.Common;
using JX.Infrastructure.Field;
using JX.Infrastructure.Framework.Authorize;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace JXWebSite.Areas.Admin.Controllers
{
	/// <summary>
	/// 模型管理模块控制器
	/// </summary>
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    [AdminAuthorize]
    public class CommonModelController : Controller
    {
        private IModelsServiceAppDapper _ModelsService;
		private IModelTemplatesServiceAppDapper _ModelTemplatesService;
		private IUserGroupsServiceAppDapper _UserGroupsServiceApp;
		private IRolesServiceAppDapper _RolesServiceApp;
		/// <summary>
		/// 依赖注入
		/// </summary>
		/// <param name="ModelsService"></param>
		/// <param name="ModelTemplatesService"></param>
		/// <param name="UserGroupsServiceApp"></param>
		/// <param name="RolesServiceApp"></param>
		public CommonModelController(IModelsServiceAppDapper ModelsService, 
			IModelTemplatesServiceAppDapper ModelTemplatesService, 
			IUserGroupsServiceAppDapper UserGroupsServiceApp, 
			IRolesServiceAppDapper RolesServiceApp)
        {
            _ModelsService = ModelsService;
			_ModelTemplatesService = ModelTemplatesService;
			_UserGroupsServiceApp = UserGroupsServiceApp;
			_RolesServiceApp = RolesServiceApp;
		}

		#region 内容模型
		/// <summary>
		/// 内容模型列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ContentModel")]
		public IActionResult ContentModel()
		{
			return View();
		}

		/// <summary>
		/// 得到内容模型的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ContentModel")]
		public IActionResult GetContentModelList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			int TabStatus = Utility.Query("TabStatus", 0);
			ModelShowType showType = ModelShowType.None;
			switch (TabStatus)
			{
				case 1:
					showType = ModelShowType.Enable;
					break;
				case 2:
					showType = ModelShowType.Disable;
					break;
			}
			var result = _ModelsService.GetContentModelList(showType).ToList();
			var pagerModel = new PagerModel<ModelsEntity>(PageNum, PageSize, result.Count, result);
			return Json(pagerModel);
		}

		/// <summary>
		/// 删除内容模型
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ContentModel,ProductModelManage")]
		public IActionResult DelModel(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				int result = _ModelsService.DeleteFull(id);
				string strMsg = "删除模型错误";
				switch (result)
				{
					case 0:
						strMsg = "ok";
						break;
					case 1:
						strMsg = "删除的模型中包含有数据";
						break;
					case 2:
						strMsg = "删除模型中存在订单";
						break;
					case 3:
						strMsg = "删除模型错误";
						break;
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新模型状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="Status"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ContentModel,ProductModelManage")]
		public IActionResult UpdateModelStatus(int id,bool Status)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要操作的记录ID！"
				});
			}
			try
			{
				if (_ModelsService.Disable(id, Status))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "更新失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		#endregion

		#region 商品模型
		/// <summary>
		/// 商品模型列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ProductModelManage")]
		public IActionResult ProductModel()
		{
			return View();
		}

		/// <summary>
		/// 得到商品模型的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ProductModelManage")]
		public IActionResult GetShopModelList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			int TabStatus = Utility.Query("TabStatus", 0);
			ModelShowType showType = ModelShowType.None;
			switch (TabStatus)
			{
				case 1:
					showType = ModelShowType.Enable;
					break;
				case 2:
					showType = ModelShowType.Disable;
					break;
			}
			var result = _ModelsService.GetShopModelList(showType).ToList();
			var pagerModel = new PagerModel<ModelsEntity>(PageNum, PageSize, result.Count, result);
			return Json(pagerModel);
		}
		#endregion

		#region 模型编辑
		/// <summary>
		/// 模型编辑
		/// </summary>
		/// <param name="id"></param>
		/// <param name="ModelType">模型类型（1：内容模型；2：商品模型；）</param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ContentModel,ProductModelManage")]
		public IActionResult ModelEdit(int id = 0, int ModelType = 1)
		{
			var model = new ModelsEntity();
			model.ModelID = id;
			if (id <= 0)
			{
				List<ModelTemplatesEntity> list = null;
				if (ModelType == 2)
				{
					list = _ModelTemplatesService.GetList(p => p.IsEshop == true).ToList();
					model.ModelType = 2;
				}
				else
				{
					list = _ModelTemplatesService.GetList(p => p.IsEshop == false).ToList();
					model.ModelType = 1;
				}
				if (list == null)
				{
					list = new List<ModelTemplatesEntity>();
				}
				list.Insert(0, new ModelTemplatesEntity { TemplateID = 0, IsEshop = (ModelType == 2) ? true : false, TemplateName = "空白模板" });
				var selectList = new SelectList(list, "TemplateID", "TemplateName");
				var selectItemList = new List<SelectListItem>();
				selectItemList.AddRange(selectList);
				ViewBag.ModelTemplatesList = selectItemList;
				return View(model);
			}
			else
			{
				model = _ModelsService.Get(id);
				if (model == null)
				{
					Utility.WriteMessage("没有找到指定的模型", "mClose");
					return View(model);
				}
			}
			return View(model);
		}

		/// <summary>
		/// 模型编辑
		/// </summary>
		/// <param name="model"></param>
		/// <param name="form"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ContentModel,ProductModelManage")]
		public ActionResult ModelEdit(ModelsEntity model, IFormCollection form = null)
		{
			if (model.ModelID.ToInt32() <= 0)
			{
				if (_ModelsService.ModelNameExists(model.ModelName))
				{
					Utility.WriteMessage("已经存在此模型名称", "mClose");
					return View(model);
				}
				string TableName = model.TableName;
				model.TableName = "U_" + TableName;
				if (_ModelsService.TableNameExists(model.TableName))
				{
					model.TableName = TableName;
					Utility.WriteMessage("已经存在要创建的表名", "mClose");
					return View(model);
				}
				int modelTemplateId = DataConverter.CLng(form["dropModelTemplate"]);
				if (!_ModelsService.Add(model, modelTemplateId))
				{
					Utility.WriteMessage("添加失败", "mClose");
					return View(model);
				}
			}
			else
			{
				if(_ModelsService.IsExist(p=>p.ModelName==model.ModelName && p.ModelID != model.ModelID))
				{
					ModelState.AddModelError(string.Empty, "已经存在此模型名称");
					return View(model);
				}
				var oldModel = _ModelsService.Get(model.ModelID);
				if (oldModel == null)
				{
					Utility.WriteMessage("没有找到要修改的模型", "mClose");
					return View(model);
				}
				model.Field = oldModel.Field;
				if (!_ModelsService.Update(model))
				{
					Utility.WriteMessage("编辑失败", "mClose");
					return View(model);
				}
			}
			Utility.WriteMessage("编辑成功", "mRefresh");
			return View(model);
		}
		#endregion

		#region 模型字段管理
		/// <summary>
		/// 模型字段列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult FieldManage(int ModelType = -1, int ModelID = 0)
		{
			var myModel = _ModelsService.Get(ModelID);
			if (myModel == null && !Enum.IsDefined(typeof(ModelType), ModelType))
			{
				Utility.WriteMessage("模型不存在", "mClose");
				return View();
			}
			ViewBag.ModelType = ModelType;
			ViewBag.ModelID = ModelID;
			return View();
		}

		/// <summary>
		/// 得到模型字段的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult GetFieldList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			int ModelType = Utility.Query("ModelType", 0);
			int ModelID = Utility.Query("ModelID", 0);
			List<FieldInfo> result = new List<FieldInfo>();
			switch (ModelType)
			{
				case 1:
				case 2:
					result = _ModelsService.GetFieldListByModelId(ModelID).ToList();
					break;
				case 3:
				case 4:
				case 5:
					result = _ModelsService.GetFieldListByModelType((ModelType)ModelType).ToList();
					break;
			}
			var pagerModel = new PagerModel<FieldInfo>(PageNum, PageSize, result.Count, result);
			return Json(pagerModel);
		}
		/// <summary>
		/// 获取字段别名及字段名数据表
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult GetFieldNamesByTextType()
		{
			int ModelID = Utility.Query("ModelID", 0);
			FieldType[] fieldType = new FieldType[] { FieldType.TextType, FieldType.TitleType };
			var result = _ModelsService.GetFieldNames(ModelID, fieldType);
			List<object> dataList = new List<object>();
			foreach (DataRow row in result.Rows)
			{
				dataList.Add(new
				{
					FieldName = row["FieldName"].ToString(),
					FieldAlias = row["FieldAlias"].ToString()
				});
			}
			return Json(dataList);
		}

		/// <summary>
		/// 删除字段
		/// </summary>
		/// <param name="ModelType"></param>
		/// <param name="ModelID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult DelField(int ModelType = 0, int ModelID = 0,string FieldName="")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (ModelID <= 0)
						{
							return Json(new {Result = "删除失败！没有指定要删除的模型！"});
						}
						flag = _ModelsService.DeleteFieldToTable(FieldName, ModelID);
						break;
					case 3:
					case 4:
					case 5:
						flag = _ModelsService.DeleteFieldToTable(FieldName, (ModelType)ModelType);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "删除失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新字段状态
		/// </summary>
		/// <param name="Disabled">是否禁用</param>
		/// <param name="ModelType"></param>
		/// <param name="ModelID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult UpdateFieldStatus(bool Disabled,int ModelType = 0, int ModelID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (ModelID <= 0)
						{
							return Json(new { Result = "更新失败！没有指定要更新状态的模型！" });
						}
						flag = _ModelsService.SetFieldDisabled(FieldName, Disabled, ModelID);
						break;
					case 3:
					case 4:
					case 5:
						flag = _ModelsService.SetFieldDisabled(FieldName, Disabled,(ModelType)ModelType);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "更新失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新字段必填属性
		/// </summary>
		/// <param name="EnableNull">是否必填</param>
		/// <param name="ModelType"></param>
		/// <param name="ModelID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult UpdateFieldRequired(bool EnableNull, int ModelType = 0, int ModelID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (ModelID <= 0)
						{
							return Json(new { Result = "更新失败！没有指定要更新状态的模型！" });
						}
						flag = _ModelsService.SetFieldEnableNull(FieldName, EnableNull, ModelID);
						break;
					case 3:
					case 4:
					case 5:
						flag = _ModelsService.SetFieldEnableNull(FieldName, EnableNull, (ModelType)ModelType);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "更新失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新字段允许搜索属性
		/// </summary>
		/// <param name="EnableShowOnSearchForm">是否允许搜索</param>
		/// <param name="ModelType"></param>
		/// <param name="ModelID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult UpdateFieldSearch(bool EnableShowOnSearchForm, int ModelType = 0, int ModelID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (ModelID <= 0)
						{
							return Json(new { Result = "更新失败！没有指定要更新状态的模型！" });
						}
						flag = _ModelsService.SetFieldEnableSearch(FieldName, EnableShowOnSearchForm, ModelID);
						break;
					case 3:
					case 4:
					case 5:
						flag = _ModelsService.SetFieldEnableSearch(FieldName, EnableShowOnSearchForm, (ModelType)ModelType);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "更新失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 重设模型字段顺序
		/// </summary>
		/// <param name="ModelType"></param>
		/// <param name="ModelID"></param>
		/// <param name="datas">要排序的字段名集合，用“,”分隔</param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult SetFieldOrderSort(int ModelType = 0, int ModelID = 0, string datas = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "排序失败！没有指定要排序的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(datas))
			{
				return Json(new
				{
					Result = "排序失败！没有指定要排序的数据！"
				});
			}
			try
			{
				List<FieldInfo> fieldInfoList = new List<FieldInfo>();
				var dataList = StringHelper.GetArrayBySplit<string>(datas.Trim(','));
				for (int i = 0; i < dataList.Count; i++)
				{
					FieldInfo item = new FieldInfo();
					item.OrderId = i+1;
					item.Id = dataList[i];
					fieldInfoList.Add(item);
				}
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (ModelID <= 0)
						{
							return Json(new { Result = "排序失败！没有指定要排序的模型！" });
						}
						flag = _ModelsService.SetOrderId(fieldInfoList, ModelID);
						break;
					case 3:
					case 4:
					case 5:
						flag = _ModelsService.SetOrderId(fieldInfoList, (ModelType)ModelType);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "排序失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "排序失败！" + ex.Message
				});
			}
		}

		/// <summary>
		/// 模型预览页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult ModelPreView(int ModelType = -1, int ModelID = 0)
		{
			var myModel = _ModelsService.Get(ModelID);
			if (myModel == null && !Enum.IsDefined(typeof(ModelType), ModelType))
			{
				Utility.WriteMessage("模型不存在", "mClose");
				return View();
			}
			ViewBag.ModelType = ModelType;
			ViewBag.ModelID = ModelID;
			List<FieldInfo> result = new List<FieldInfo>();
			switch (ModelType)
			{
				case 1:
				case 2:
					result = _ModelsService.GetFieldListByModelId(ModelID).ToList();
					break;
				case 3:
				case 4:
				case 5:
					result = _ModelsService.GetFieldListByModelType((ModelType)ModelType).ToList();
					break;
			}
			return View(result);
		}
		#endregion

		#region 模型字段编辑
		/// <summary>
		/// 模型字段编辑
		/// </summary>
		/// <param name="mAction">Add：添加；Modify：修改；Copy：复制；</param>
		/// <param name="ModelType"></param>
		/// <param name="ModelID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult FieldEdit(string mAction = "Add",int ModelType = 0, int ModelID = 0,string FieldName="")
		{
			ViewBag.mAction = mAction;
			ViewBag.ModelType = ModelType;
			ViewBag.ModelID = ModelID;
			var model = new FieldInfo();
			switch (mAction)
			{
				case "Modify":
				case "Copy":
					if (string.IsNullOrEmpty(FieldName))
					{
						Utility.WriteMessage("字段名不能为空", "mClose");
						return View(model);
					}
					switch (ModelType)
					{
						case 1:
						case 2:
							model = _ModelsService.GetFieldInfoByFieldName(ModelID, FieldName);
							break;
						case 3:
						case 4:
						case 5:
							model = _ModelsService.GetFieldInfoByFieldName((ModelType)ModelType,FieldName);
							break;
					}
					if (model == null || model.IsNull==true)
					{
						Utility.WriteMessage("字段不存在", "mClose");
						return View(model);
					}
					if (mAction == "Copy")
					{
						model.FieldName = string.Empty;
						model.FieldAlias = string.Empty;
					}
					break;
				default:
					model.FieldLevel = 1;
					model.FieldType = FieldType.TextType;
					break;
			}
			return View(model);
		}

		/// <summary>
		/// 模型字段编辑
		/// </summary>
		/// <param name="model"></param>
		/// <param name="form"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public ActionResult FieldEdit(FieldInfo model, IFormCollection form = null)
		{
			string mAction = form["mAction"].ToString();
			int mModelType = form["ModelType"].ToInt32();
			int ModelID = form["ModelID"].ToInt32();
			if(ModelID <= 0)
			{
				Utility.WriteMessage("模型ID不能为空", "mClose");
				return View(model);
			}
			#region 得到字段控件的值
			int orderId = 0;
			FieldType fieldType = (FieldType)Enum.Parse(typeof(FieldType), form["FieldType"].ToString());
			IList<FieldInfo> fieldList = _ModelsService.GetFieldListByModelId(ModelID);
			foreach (FieldInfo info in fieldList)
			{
				if (info.FieldType == fieldType)
				{
					switch (fieldType)
					{
						case FieldType.IPType:
							Utility.WriteMessage("模型中已经存在有IP地址字段了！", "mClose");
							return View(model);

						case FieldType.UserNameType:
							Utility.WriteMessage("模型中已经存在有用户名字段了！", "mClose");
							return View(model);
					}
				}
				if (info.OrderId > orderId)
				{
					orderId = info.OrderId;
				}
			}
			orderId++;
			FieldInfo fieldInfo = new FieldInfo();
			fieldInfo = model;
			fieldInfo.Id = fieldInfo.FieldName;
			fieldInfo.OrderId = orderId;
			GetSettingsByFieldType(fieldType, form, fieldInfo);
			#endregion
			ResultInfo unknown = new ResultInfo();
			switch (mAction)
			{
				case "Modify":
					#region 修改
					fieldInfo.OrderId = form["OrderId"].ToInt32();
					if (mModelType == 3 || mModelType == 4 || mModelType == 5)
					{
						if(_ModelsService.UpdateFieldOfTable(fieldInfo, (ModelType)mModelType))
						{
							unknown.Status = 1;
							ModelID = _ModelsService.GetModelByModelType((ModelType)mModelType).ModelID.Value;
						}
						else
						{
							unknown.Msg = "更新字段失败";
						}
					}
					else
					{
						if(_ModelsService.UpdateFieldOfTable(fieldInfo, ModelID))
						{
							unknown.Status = 1;
						}
						else
						{
							unknown.Msg = "更新字段失败";
						}
					}
					#endregion
					break;
				case "Copy":
					#region 复制
					fieldInfo.FieldLevel = 1;
					if (mModelType == 3 || mModelType == 4 || mModelType == 5)
					{
						unknown = _ModelsService.AddFieldToTable(fieldInfo, (ModelType)mModelType);
						ModelID = _ModelsService.GetModelByModelType((ModelType)mModelType).ModelID.Value;
					}
					else
					{
						unknown = _ModelsService.AddFieldToTable(fieldInfo, ModelID);
					}
					#endregion
					break;
				default:
					#region 添加
					fieldInfo.FieldLevel = 1;
					if (mModelType == 3 || mModelType == 4 || mModelType == 5)
					{
						unknown = _ModelsService.AddFieldToTable(fieldInfo, (ModelType)mModelType);
						ModelID = _ModelsService.GetModelByModelType((ModelType)mModelType).ModelID.Value;
					}
					else
					{
						unknown = _ModelsService.AddFieldToTable(fieldInfo, ModelID);
					}
					#endregion
					break;
			}
			if (unknown.Status == 1)
			{
				string strGroupIDs = form["ctlGroupList"].ToString();
				string strRoleIDs = form["ctlRoleList"].ToString();
				_UserGroupsServiceApp.AddFieldPermissionToUserGroup(strGroupIDs, ModelID, fieldInfo.FieldName, OperateCode.ContentFieldInput, 1);
				_RolesServiceApp.AddFieldPermissionToRoles(strRoleIDs, ModelID, fieldInfo.FieldName, OperateCode.ContentFieldInput);
				Utility.WriteMessage("编辑成功", "mRefresh");
			}
			else
			{
				Utility.WriteMessage(unknown.Msg, "mClose");
			}
			return View(model);
		}
		/// <summary>
		/// 根据字段类型，得到对应的自定义属性
		/// </summary>
		/// <param name="fieldType"></param>
		/// <param name="form"></param>
		/// <param name="fieldInfo"></param>
		/// <returns></returns>
		private void GetSettingsByFieldType(FieldType fieldType,IFormCollection form, FieldInfo fieldInfo)
		{
			fieldInfo.DefaultValue = DataConverter.ToString(form["ctl" + fieldType.ToString() + "DefaultValue"]);
			Collection<string> collection = new Collection<string>();
			switch (fieldType)
			{
				case FieldType.TextType:
					collection.Add(form["ctlTextTypeMaxLength"].ToInt32(200).ToString().Trim());
					collection.Add("100%");
					collection.Add("False");
					collection.Add("0");
					collection.Add(form["InsideLink"].ToBoolean().ToString());
					collection.Add(form["FilterWord"].ToBoolean().ToString());
					collection.Add(form["ShieldWord"].ToBoolean().ToString());
					collection.Add(form["ctlTextTypeRegexRule"].ToString().Trim());
					collection.Add(form["ctlTextTypeRegexErrorMsg"].ToString().Trim());
					break;

				case FieldType.MultipleTextType:
					collection.Add("100%");
					collection.Add("100%");
					collection.Add(form["InsideLink"].ToBoolean().ToString());
					collection.Add(form["FilterWord"].ToBoolean().ToString());
					collection.Add(form["ShieldWord"].ToBoolean().ToString());
					break;

				case FieldType.MultipleHtmlTextType:
					collection.Add(form["ctlEditorType"].ToString().Trim());
					collection.Add("100%");
					collection.Add("100%");
					collection.Add(form["InsideLink"].ToBoolean().ToString());
					collection.Add(form["FilterWord"].ToBoolean().ToString());
					collection.Add(form["ShieldWord"].ToBoolean().ToString());
					break;

				case FieldType.IntegerType:
					collection.Add(form["ctlIntegerTypeMinLength"].ToInt32(0).ToString().Trim());
					collection.Add(form["ctlIntegerTypeMaxLength"].ToInt32(int.MaxValue).ToString().Trim());
					collection.Add(form["ctlIntegerTypePercent"].ToBoolean().ToString());
					break;

				case FieldType.NumberType:
					collection.Add(form["ctlNumberTypeMinLength"].ToInt32(0).ToString().Trim());
					collection.Add(form["ctlNumberTypeMaxLength"].ToInt32(int.MaxValue).ToString().Trim());
					collection.Add(form["ctlNumberTypeDecimals"].ToInt32(-1).ToString().Trim());
					collection.Add(form["ctlNumberTypePercent"].ToBoolean().ToString());
					break;

				case FieldType.MoneyType:
					collection.Add(form["ctlMoneyTypeMinLength"].ToInt32(0).ToString().Trim());
					collection.Add(form["ctlMoneyTypeMaxLength"].ToInt32(int.MaxValue).ToString().Trim());
					break;

				case FieldType.FileType:
					var FileTypeFileNameRule = form["ctlFileTypeFileNameRule"].ToString().Trim();
					Regex regexFileNameRule = new Regex(@"^([_a-zA-Z0-9]|\{\$Random\}|\{\$Year\}|\{\$Month\}|\{\$Day\}|\{\$Hour\}|\{\$Minute\}|\{\$Second\}|\{\$Origin\}){1,}$");
					if (!regexFileNameRule.IsMatch(FileTypeFileNameRule))
					{
						Utility.WriteMessage("上传文件名保存规则：文件名只能以字母、数字、下划线及可用变量组成", "mClose");
						return;
					}
					var FileTypeSizeFieldName = form["ctlFileTypeSizeFieldName"].ToString().Trim();
					Regex regexFileTypeSizeFieldName = new Regex(RegexHelper.FieldNamePattern);
					if (!regexFileTypeSizeFieldName.IsMatch(FileTypeSizeFieldName))
					{
						Utility.WriteMessage("保存文件大小的字段名：由字母、数字、下划线组成，并且仅能字母开头，不以下划线结尾", "mClose");
						return;
					}
					if (FileTypeSizeFieldName== fieldInfo.FieldName)
					{
						Utility.WriteMessage("保存文件大小的字段名不能与主字段名重复", "mClose");
						return;
					}
					bool IsSaveFileSize = form["ctlFileTypeIsSaveFileSize"].ToBoolean();
					if (IsSaveFileSize && string.IsNullOrEmpty(FileTypeSizeFieldName))
					{
						Utility.WriteMessage("记录文件大小字段名不能为空", "mClose");
						return;
					}
					collection.Add(form["ctlFileTypeFileSize"].ToInt32(10240).ToString());
					collection.Add(form["ctlFileTypeFileExts"].ToString().Trim());
					collection.Add(form["ctlFileTypeIsDownLoad"].ToBoolean().ToString());
					collection.Add(IsSaveFileSize.ToString());
					collection.Add(FileTypeSizeFieldName);
					collection.Add(FileTypeFileNameRule);
					collection.Add(form["ctlFileTypeFileUrlPrdfix"].ToString().Trim());
					collection.Add(form["ctlFileTypeFileMaxCount"].ToInt32(1).ToString());
					break;

				case FieldType.PictureType:
					collection.Add("100%");
					collection.Add(form["ctlPictureTypeImageSize"].ToInt32(10240).ToString());
					collection.Add(form["ctlPictureTypeImageExts"].ToString().Trim());
					collection.Add("false");
					collection.Add(form["ctlPictureTypeIsWaterMark"].ToBoolean().ToString());
					collection.Add(form["ctlPictureTypeIsThumb"].ToBoolean().ToString());
					collection.Add("true");
					collection.Add("false");
					collection.Add("{$Random}");
					break;

				case FieldType.MultiplePhotoType:
					collection.Add(form["ctlMultiplePhotoTypeImageSize"].ToInt32(10240).ToString());
					collection.Add(form["ctlMultiplePhotoTypeImageExts"].ToString().Trim());
					collection.Add(form["ctlMultiplePhotoTypeIsWaterMark"].ToBoolean().ToString());
					collection.Add("{$Random}");
					break;

				case FieldType.ListBoxType:
					var ListBoxTypeDataList = DataConverter.ToString(form["hidListBoxTypeDataList"]);
					if (string.IsNullOrEmpty(ListBoxTypeDataList))
					{
						Utility.WriteMessage("请输入选项", "mClose");
						return;
					}
					collection.Add(ListBoxTypeDataList);
					collection.Add(form["ctlListBoxTypeShowType"].ToInt32(1).ToString());
					collection.Add("false");
					collection.Add(form["ctlListBoxTypeRepeatColumns"].ToInt32(1).ToString());
					break;

				case FieldType.ListBoxIntroType:
					var ListBoxIntroTypeDataList = DataConverter.ToString(form["hidListBoxIntroTypeDataList"]);
					if (string.IsNullOrEmpty(ListBoxIntroTypeDataList))
					{
						Utility.WriteMessage("请输入选项", "mClose");
						return;
					}
					collection.Add(ListBoxIntroTypeDataList);
					collection.Add(form["ctlListBoxIntroTypeShowType"].ToInt32(1).ToString());
					collection.Add(form["ctlListBoxIntroTypeRepeatColumns"].ToInt32(1).ToString());
					break;

				case FieldType.ListBoxDataType:
					collection.Add(form["ctlListBoxDataTypeSQL"].ToString().Trim());
					collection.Add(form["ctlListBoxDataTypeTextField"].ToString().Trim());
					collection.Add(form["ctlListBoxDataTypeValueField"].ToString().Trim());
					collection.Add(form["ctlListBoxDataTypeValueFieldType"].ToString().Trim());
					collection.Add(form["ctlListBoxDataTypeShowType"].ToInt32(1).ToString());
					collection.Add(form["ctlListBoxDataTypeIsFillData"].ToBoolean().ToString());
					collection.Add(form["ctlListBoxDataTypeRepeatColumns"].ToInt32(1).ToString());
					break;

				case FieldType.BoolType:
					collection.Add(form["ctlBoolTypeTrueText"].ToString().Trim());
					collection.Add(form["ctlBoolTypeFalseText"].ToString().Trim());
					break;

				case FieldType.DateTimeType:
					collection.Add(form["ctlDateTimeTypeFormat"].ToString().Trim());
					collection.Add(form["ctlDateTimeTypeDefaultType"].ToInt32(1).ToString());
					collection.Add(form["ctlDateTimeTypeIsRange"].ToBoolean().ToString());
					break;

				case FieldType.LookType:
					collection.Add(form["ctlLookTypeModelID"].ToInt32().ToString());
					collection.Add(form["ctlLookTypeFieldName"].ToString().Trim());
					break;

				case FieldType.OperatingType:
					var OperatingTypeSelectItem = form["ctlOperatingTypeSelectItem"].ToString().Trim();
					OperatingTypeSelectItem = OperatingTypeSelectItem.Replace("\r\n", "$$$");
					collection.Add(OperatingTypeSelectItem);
					collection.Add("100%");
					collection.Add(form["ctlOperatingTypeIsSave"].ToBoolean().ToString());
					break;

				case FieldType.RegionType:
					collection.Add("true");
					collection.Add(form["ctlRegionTypeShowType"].ToInt32(5).ToString());
					string RegionTypeProvince = form["RegionType.Province"].ToString().Trim();
					string RegionTypeCity = form["RegionType.City"].ToString().Trim();
					string RegionTypeArea = form["RegionType.Area"].ToString().Trim();
					string RegionTypeSelectValue = "中华人民共和国," + RegionTypeProvince + "," + RegionTypeCity + "," + RegionTypeArea;
					collection.Add(RegionTypeSelectValue);
					break;

				case FieldType.RegionTypeText:
					collection.Add("true");
					collection.Add(form["ctlRegionTypeTextShowType"].ToInt32(5).ToString());
					string RegionTypeTextProvince = form["RegionTypeText.Province"].ToString().Trim();
					string RegionTypeTextCity = form["RegionTypeText.City"].ToString().Trim();
					string RegionTypeTextArea = form["RegionTypeText.Area"].ToString().Trim();
					string RegionTypeTextAddress = form["ctlRegionTypeTextSelectAddress"].ToString().Trim();
					string RegionTypeTextSelectValue = "中华人民共和国," + RegionTypeTextProvince + "," + RegionTypeTextCity + "," + RegionTypeTextArea + "," + RegionTypeTextAddress;
					collection.Add(RegionTypeTextSelectValue);
					break;

				case FieldType.RegionTypeFive:
					collection.Add("true");
					collection.Add(form["ctlRegionTypeFiveShowType"].ToInt32(5).ToString());
					string RegionTypeFiveProvince = form["RegionTypeFive.Province"].ToString().Trim();
					string RegionTypeFiveCity = form["RegionTypeFive.City"].ToString().Trim();
					string RegionTypeFiveArea = form["RegionTypeFive.Area"].ToString().Trim();
					string RegionTypeFiveSelectValue = "中华人民共和国," + RegionTypeFiveProvince + "," + RegionTypeFiveCity + "," + RegionTypeFiveArea + ",,";
					collection.Add(RegionTypeFiveSelectValue);
					break;

				case FieldType.RegionTypeSelect:
					string RegionTypeSelectProvince = form["RegionTypeSelect.Province"].ToString().Trim();
					string RegionTypeSelectCity = form["RegionTypeSelect.City"].ToString().Trim();
					string RegionTypeSelectArea = form["RegionTypeSelect.Area"].ToString().Trim();
					string RegionTypeSelectSelectValue = "中华人民共和国," + RegionTypeSelectProvince + "," + RegionTypeSelectCity + "," + RegionTypeSelectArea;
					collection.Add(RegionTypeSelectSelectValue);
					break;

				case FieldType.IndustryCategory:
					collection.Add(form["ctlIndustryCategoryRootID"].ToInt32().ToString());
					break;

				case FieldType.Property:
				case FieldType.ProductStyle:
					string SelectValue = DataConverter.ToString(form["ctl" + fieldType.ToString() + "SelectValue"]);
					if (SelectValue.Contains("$") || SelectValue.Contains("*"))
					{
						Utility.WriteMessage("输入的值中不能带有半角的“$”、“|”、“*”字符！", "mClose");
						return;
					}
					collection.Add(SelectValue);
					break;

				case FieldType.TitleType:
					collection.Add(form["ctlTitleTypeMaxLength"].ToInt32(200).ToString());
					collection.Add("100%");
					collection.Add(form["ctlTitleTypeCheckIsExist"].ToBoolean().ToString());
					collection.Add(form["ctlTitleTypeCreatePY"].ToBoolean().ToString());
					break;

			}
			fieldInfo.CopyToSettings(collection);
		}
		#endregion

		#region 模型模板
		/// <summary>
		/// 模型模板列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult ModelTemplateManage()
		{
			return View();
		}

		/// <summary>
		/// 得到模型模板的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult GetModelTemplateList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			int TabStatus = Utility.Query("TabStatus", 0);
			string filter = " 1=1 ";
			switch (TabStatus)
			{
				case 1://内容模型模板
					filter += " and IsEshop=0 ";
					break;
				case 2://商品模型模板
					filter += " and IsEshop=1 ";
					break;
			}
			string strColumn = " * ";
			string TableName = " ModelTemplates ";
			int RecordTotal;
			var result = _ModelTemplatesService.GetListPaged<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumn: "TemplateID", Sorts: "desc", out RecordTotal).ToList();
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			return Json(pagerModel);
		}

		/// <summary>
		/// 删除模型模板
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult DelModelTemplate(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_ModelTemplatesService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		#endregion

		#region 模型模板编辑
		/// <summary>
		/// 模型模板编辑
		/// </summary>
		/// <param name="id">模板ID：小于等于0表示添加新模板；大于0表示编辑模板；</param>
		/// <param name="ModelID">用来生成模板的模型ID</param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult ModelTemplateEdit(int id = 0,int ModelID = 0)
		{
			ViewBag.ModelID = ModelID;
			var model = new ModelTemplatesEntity();
			model.TemplateID = id;
			if (id > 0)
			{
				model = _ModelTemplatesService.Get(id);
				if(model == null)
				{
					Utility.WriteMessage("没有找到指定的模板", "mClose");
					return View(model);
				}
			}
			else
			{
				if (ModelID > 0)
				{
					var modelEntity = _ModelsService.Get(ModelID);
					if (modelEntity != null && modelEntity.ModelType == 2)
					{
						model.IsEshop = true;
					}
				}
			}
			return View(model);
		}

		/// <summary>
		/// 模型模板编辑
		/// </summary>
		/// <param name="model"></param>
		/// <param name="form"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public ActionResult ModelTemplateEdit(ModelTemplatesEntity model, IFormCollection form = null)
		{
			if (model.TemplateID.ToInt32() <= 0)
			{
				if (_ModelTemplatesService.IsExist(p => p.TemplateName == model.TemplateName))
				{
					Utility.WriteMessage("已经存在此模板名称", "mClose");
					return View(model);
				}
				int ModelID = DataConverter.CLng(form["ModelID"]);
				if(ModelID > 0)
				{
					model.Field = _ModelsService.Get(ModelID)?.Field;
				}
				else
				{
					model.Field = (!model.IsEshop) ? _ModelsService.AddDefaultField() : string.Empty;
				}
				if (!_ModelTemplatesService.Add(model))
				{
					Utility.WriteMessage("添加失败", "mClose");
					return View(model);
				}
			}
			else
			{
				if (_ModelTemplatesService.IsExist(p => p.TemplateName == model.TemplateName && p.TemplateID != model.TemplateID))
				{
					ModelState.AddModelError(string.Empty, "已经存在此模板名称");
					return View(model);
				}
				var oldModel = _ModelTemplatesService.Get(model.TemplateID);
				if (oldModel == null)
				{
					Utility.WriteMessage("没有找到要修改的模板", "mClose");
					return View(model);
				}
				model.Field = oldModel.Field;
				if (!_ModelTemplatesService.Update(model))
				{
					Utility.WriteMessage("编辑失败", "mClose");
					return View(model);
				}
			}
			Utility.WriteMessage("编辑成功", "mRefresh");
			return View(model);
		}
		#endregion

		#region 模型模板字段管理
		/// <summary>
		/// 模型模板字段列表页面
		/// </summary>
		/// <param name="ModelType"></param>
		/// <param name="TemplateID"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult TemplateFieldManage(int ModelType = -1, int TemplateID = 0)
		{
			var myModel = _ModelTemplatesService.Get(TemplateID);
			if (myModel == null && !Enum.IsDefined(typeof(ModelType), ModelType))
			{
				Utility.WriteMessage("模型模板不存在", "mClose");
				return View();
			}
			ViewBag.ModelType = ModelType;
			ViewBag.TemplateID = TemplateID;
			return View();
		}

		/// <summary>
		/// 得到模型模板字段的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult GetTemplateFieldList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			int ModelType = Utility.Query("ModelType", 0);
			int TemplateID = Utility.Query("TemplateID", 0);
			List<FieldInfo> result = new List<FieldInfo>();
			switch (ModelType)
			{
				case 1:
				case 2:
					result = _ModelsService.GetFieldListByTemplateId(TemplateID).ToList();
					break;
			}
			var pagerModel = new PagerModel<FieldInfo>(PageNum, PageSize, result.Count, result);
			return Json(pagerModel);
		}

		/// <summary>
		/// 删除模型模板字段
		/// </summary>
		/// <param name="ModelType"></param>
		/// <param name="TemplateID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult DelTemplateField(int ModelType = 0, int TemplateID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (TemplateID <= 0)
						{
							return Json(new { Result = "删除失败！没有指定要删除的模型！" });
						}
						flag = _ModelsService.DeleteTemplatesField(FieldName, TemplateID);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "删除失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新字段状态
		/// </summary>
		/// <param name="Disabled">是否禁用</param>
		/// <param name="ModelType"></param>
		/// <param name="TemplateID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult UpdateTemplateFieldStatus(bool Disabled, int ModelType = 0, int TemplateID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (TemplateID <= 0)
						{
							return Json(new { Result = "更新失败！没有指定要更新状态的模型！" });
						}
						flag = _ModelsService.SetTemplateFieldDisabled(FieldName, Disabled, TemplateID);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "更新失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新字段必填属性
		/// </summary>
		/// <param name="EnableNull">是否必填</param>
		/// <param name="ModelType"></param>
		/// <param name="TemplateID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult UpdateTemplateFieldRequired(bool EnableNull, int ModelType = 0, int TemplateID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (TemplateID <= 0)
						{
							return Json(new { Result = "更新失败！没有指定要更新状态的模型！" });
						}
						flag = _ModelsService.SetTemplateFieldEnableNull(FieldName, EnableNull, TemplateID);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "更新失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新字段允许搜索属性
		/// </summary>
		/// <param name="EnableShowOnSearchForm">是否允许搜索</param>
		/// <param name="ModelType"></param>
		/// <param name="TemplateID"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult UpdateTemplateFieldSearch(bool EnableShowOnSearchForm, int ModelType = 0, int TemplateID = 0, string FieldName = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(FieldName))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要更新状态的模型字段名称！"
				});
			}
			try
			{
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (TemplateID <= 0)
						{
							return Json(new { Result = "更新失败！没有指定要更新状态的模型！" });
						}
						flag = _ModelsService.SetTemplateFieldEnableSearch(FieldName, EnableShowOnSearchForm, TemplateID);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "更新失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 重设模型字段顺序
		/// </summary>
		/// <param name="ModelType"></param>
		/// <param name="TemplateID"></param>
		/// <param name="datas">要排序的字段名集合，用“,”分隔</param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ModelManage")]
		public IActionResult SetTemplateFieldOrderSort(int ModelType = 0, int TemplateID = 0, string datas = "")
		{
			if (ModelType <= 0)
			{
				return Json(new
				{
					Result = "排序失败！没有指定要排序的模型类型！"
				});
			}
			if (string.IsNullOrEmpty(datas))
			{
				return Json(new
				{
					Result = "排序失败！没有指定要排序的数据！"
				});
			}
			try
			{
				List<FieldInfo> fieldInfoList = new List<FieldInfo>();
				var dataList = StringHelper.GetArrayBySplit<string>(datas.Trim(','));
				for (int i = 0; i < dataList.Count; i++)
				{
					FieldInfo item = new FieldInfo();
					item.OrderId = i + 1;
					item.Id = dataList[i];
					fieldInfoList.Add(item);
				}
				bool flag = false;
				switch (ModelType)
				{
					case 1:
					case 2:
						if (TemplateID <= 0)
						{
							return Json(new { Result = "排序失败！没有指定要排序的模型！" });
						}
						flag = _ModelsService.SetTemplateOrderId(fieldInfoList, TemplateID);
						break;
				}
				string strMsg = "ok";
				if (!flag)
				{
					strMsg = "排序失败";
				}
				return Json(new { Result = strMsg });
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "排序失败！" + ex.Message
				});
			}
		}

		#endregion
	}
}